﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GUI.AirportSVC;
using System.Net.NetworkInformation;
using System.Diagnostics;

namespace GUI.ServiceAgents
{
    public interface IAirportServiceAgent
    {
        void ConnectToAirportService(EventHandler<newFlightUpdateReceivedEventArgs> callback);
    }

    public class AirportServiceAgent : IAirportServiceAgent
    {
        AirportSVC.AirportServiceClient c =
                    new AirportSVC.AirportServiceClient("NetTcpBinding_IAirportService");

        // Establishes connection to AirportService and receives new flights
        public void ConnectToAirportService(EventHandler<newFlightUpdateReceivedEventArgs> callback)
        {
            c.newFlightUpdateReceived += callback;
            
            // Added to debug network problems
            if (NetworkInterface.GetIsNetworkAvailable())
            {
                c.RegisterForFlightUpdatesAsync();
            }
            else
            {
                Debug.WriteLine("AirportServiceAgent: Network Unavailable");
            }
        }
        
    }
}
